import React from 'react';
import ReactDOM from 'react-dom';
import Simditor from 'simditor';
import $ from 'jquery';

export default class Editor extends React.Component {
    constructor(props,context){
        super(props, context);
    }
    componentWillMount(){
        // this.loadCount = 1;
    }

    componentDidMount(){
        this.loadCount = 1;
        let _this = this;
        let {onChange,id='defaultEditor'} = this.props;
        this.editor = CKEDITOR.replace(id);
        this.editor.on( 'change', function( event ) {   
            onChange(this.getData())
        });
    }
    componentWillMount(props){
    }
    componentWillUpdate(props){
        let {mode} = props;
        if(this.loadCount <= 1 && mode=='edit'){
            this.loadCount ++ ;
            this.timer = setTimeout(() => {
                this.editor.setData(props.value);
            },100)
        }
    }
    componentWillReceiveProps(props,context){
        
    }
    componentDidUpdate(props){
        
    }
    componentWillUnmount(){
        clearTimeout(this.timer);
    }




    render () {
        let {placeholder,id='defaultEditor',value} = this.props;
        return (
            <div className="custom-ckeditor">
                 {value?
                    <textarea id={id}   placeholder="请输入文章" >{value}</textarea>
                    :<textarea id={id}   placeholder="请输入文章" ></textarea>
                } 
                {/* <textarea id={id}   placeholder="请输入文章" ></textarea> */}
                
            </div>
        )
    }
    

}